package com.ruoyi.cms.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsSiteMapper;
import com.ruoyi.cms.domain.CmsSite;
import com.ruoyi.cms.service.ICmsSiteService;
import com.ruoyi.common.core.text.Convert;

/**
 * 站点信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-28
 */
@Service
public class CmsSiteServiceImpl implements ICmsSiteService 
{
    @Autowired
    private CmsSiteMapper cmsSiteMapper;

    /**
     * 查询站点信息
     * 
     * @param siteId 站点信息ID
     * @return 站点信息
     */
    @Override
    public CmsSite selectCmsSiteById(Long siteId)
    {
        return cmsSiteMapper.selectCmsSiteById(siteId);
    }

    @Override
    public Optional<CmsSite> selectCmsSite(Long siteId, Long userId) {
        CmsSite site = selectCmsSiteById(siteId);
        if (null != site) {
            if (userId == 1L) {
                return Optional.of(site);
            }
            int res = cmsSiteMapper.selectCmsSiteUser(siteId, userId);
            return res == 1 ? Optional.of(site) : Optional.empty();
        }
        return Optional.empty();
    }

    /**
     * 查询站点信息列表
     * 
     * @param cmsSite 站点信息
     * @return 站点信息
     */
    @Override
    public List<CmsSite> selectCmsSiteList(CmsSite cmsSite)
    {
        return cmsSiteMapper.selectCmsSiteList(cmsSite);
    }

    /**
     * 新增站点信息
     * 
     * @param cmsSite 站点信息
     * @return 结果
     */
    @Override
    public int insertCmsSite(CmsSite cmsSite)
    {
        cmsSite.setStatus("0");
        cmsSite.setDelFlag("0");
        cmsSite.setCreateTime(DateUtils.getNowDate());
        return cmsSiteMapper.insertCmsSite(cmsSite);
    }

    /**
     * 修改站点信息
     * 
     * @param cmsSite 站点信息
     * @return 结果
     */
    @Override
    public int updateCmsSite(CmsSite cmsSite)
    {
        cmsSite.setDelFlag(null);
        cmsSite.setUpdateTime(DateUtils.getNowDate());
        return cmsSiteMapper.updateCmsSite(cmsSite);
    }

    /**
     * 删除站点信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmsSiteByIds(String ids)
    {
        return cmsSiteMapper.deleteCmsSiteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除站点信息信息
     * 
     * @param siteId 站点信息ID
     * @return 结果
     */
    @Override
    public int deleteCmsSiteById(Long siteId)
    {
        return cmsSiteMapper.deleteCmsSiteById(siteId);
    }

    @Override
    public int insertCmsSiteuser(long siteId, long userId) {
        return cmsSiteMapper.insertCmsSiteUser(siteId, userId);
    }

    @Override
    public int deleteCmsSiteuser(long siteId, long userId) {
        return cmsSiteMapper.deleteCmsSiteUser(siteId, userId);
    }

    @Override
    public CmsSite switchSite(Long siteId) {
        CmsSite site = selectCmsSiteById(siteId);
        if (null == site) {
            throw new RuntimeException("站点不存在或已被删除");
        }
        ServletUtils.getSession().setAttribute("SESSION_CURR_SITE", site);
        return site;
    }

    @Override
    public CmsSite currSite() {
        CmsSite site = (CmsSite) ServletUtils.getSession().getAttribute("SESSION_CURR_SITE");
        if (null == site) {
            throw new RuntimeException("请在站点管理中执行切换站点操作");
        }
        return site;
    }

    @Override
    public long currSiteId() {
        return currSite().getSiteId();
    }
}
